<!--
	作者：offline
	时间：2018-11-28
	描述：采购订单未确认列表(lxh)
-->
<template>
	<div class="m-view-confirmedPurOrder m-window" v-loading="dataLoading">
		<!--m-view-unConfirmPurOrder-->
		<div class="m-condition m-pageHeader" v-if="diaShow">
			<el-row :gutter="5">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7">
						<el-form-item label="客户" class="rightItem">
							<el-select
								value-key="id"
								v-model="selCompanyCode"
								placeholder="请选择客户"
								@change="companyChange"
								clearable
								filterable
								:disabled="btnDis"
							>
								<el-option
									v-for="item in comOptions"
									:key="item.id"
									:label="item.companyName"
									:value="item"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="采购单号" class="rightItem">
							<el-input v-model="purOrdNo" placeholder="请输入采购订单号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="订单日期" class="rightItem">
							<el-date-picker
								v-model="beginDate"
								type="date"
								:placeholder="placeholder.start"
								:picker-options="pickerOptions"
								value-format="yyyy-MM-dd"
								:editable="false"
								:clearable="true"
							></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-date-picker
							v-model="endDate"
							type="date"
							:placeholder="placeholder.end"
							:picker-options="pickerEndOptions"
							value-format="yyyy-MM-dd"
							:editable="false"
							:clearable="true"
						></el-date-picker>
					</el-col>
				</el-form>
				<!-- <el-col :span="2" :offset="2">
          <el-button type="primary" @click="search(1)" :disabled="btnDis">查询</el-button>
				</el-col>-->
				<!-- <el-col :span="2">
					<el-button type="primary" @click="takeBack" :disabled="btnDis">退回</el-button>
				</el-col>-->
			</el-row>
			<el-row :gutter="5">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7">
						<el-form-item label="发布日期" class="rightItem">
							<el-date-picker
								v-model="releaseDate.start"
								type="date"
								:placeholder="'开始日期'"
								:picker-options="pickerOptions"
								value-format="yyyy-MM-dd"
								:editable="false"
								:clearable="true"
							></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-date-picker
							v-model="releaseDate.end"
							type="date"
							:placeholder="'结束日期'"
							:picker-options="pickerEndOptions"
							value-format="yyyy-MM-dd"
							:editable="false"
							:clearable="true"
						></el-date-picker>
					</el-col>
					<el-col :span="6">
						<el-form-item label="单据状态" class="rightItem">
							<el-select value-key="id" v-model="stateCode" :disabled="btnDis">
								<el-option
									v-for="item in stateOptions"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-form>
				<el-col :span="2" :offset="3">
					<el-button type="primary" @click="search(1)" :disabled="btnDis">查询</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-bottom m-top2" v-if="diaShow">
			<agGridComponents
				ref="agGridComponent"
				:getGridOptions="gridOptions"
				:getColumnDefs="columnDefs"
				:isColFit="true"
			></agGridComponents>
		</div>
		<div class="m-page" v-if="diaShow">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>

		<div class="m-window" v-if="componentsShow">
			<purOrderDetailsComponents
				@cancelComponents="cancelComponents"
				:getClickValue="getClickValue"
				:ordStatus="ordStatus"
			></purOrderDetailsComponents>
		</div>
	</div>
</template>

<script>
import _Api from "@/api";
import localeText from "@/i18n/agGridText";
import agGridComponents from "@/components/agGridComponents.js";
import authInfo from "@/public/authInfo.js";
import supplierInfo from "@/public/supplierInfo.js";
import buttonLink from "@/components/buttonLink.js";
import { orderType } from "@/public/enum.js";
import purOrderDetailsComponents from "@/views/purchase/purOrderDetailsComponents.vue";
import serviceTime from "@/public/serverTime.js";

export default {
	data () {
		return {
			ruleForm: {},
			selCompanyCode: "",
			comOptions: [],
			purOrdNo: "",
			beginDate: "",
			endDate: "",
			pickerOptions: {
				disabledDate: this.disabledStartDate
			},
			pickerEndOptions: {
				disabledDate: this.disabledEndDate
			},
			currentPage: 1,
			pageSize: 30,
			total: 0,
			supplierInfo: [],
			rowData: [],
			getClickValue: "",
			diaShow: true,
			componentsShow: false,
			ordStatus: "",
			current: 0,
			textChange: 0,
			btnDis: false,
			// 日期控件默认值
			placeholder: {
				start: '',
				end: ''
			},
			// 单据状态
			stateCode: 0,
			//  单据状态集合
			stateOptions: [
				{ label: '已发布', value: 0 },
				{ label: '已退回', value: 1 }
			],
			// 发布时间
			releaseDate: {
				start: '',
				end: ''
			},
			// 页面加载状态   
			dataLoading: false
		}
	},
	mounted () {
		var that = this;
		serviceTime.getServiceTime().then((serviceTime) => {
			//				that.applyDate = serviceTime.Format("yyyy-MM-dd");
			that.beginDate = new Date((serviceTime.getTime() - 604800000)).Format("yyyy-MM-dd");
			that.endDate = serviceTime.Format("yyyy-MM-dd");
			that.placeholder.start = "开始日期"
			that.placeholder.end = "结束日期"
		});
		if (this.$global.user.userTypeId == "4") {
			//      		supplierInfo.getUserInfo(this.$global.user.userName).then((supplierInfo)=>{
			//      			that.supplierInfo = supplierInfo;
			//      		})
			//				var arrayList = [];
			//				arrayList.push(this.$global.supplierData);
			this.supplierInfo.push(this.$global.supplierData);
			if (this.supplierInfo[0] == null) {
				this.dataLoading = true
				supplierInfo.getUserInfo(this.$global.user.userName).then((supplierInfo) => {
					that.dataLoading = false
					that.supplierInfo = supplierInfo
				}).catch(err => {
					that.dataLoading = false
					that.supplierInfo = []
					that.$alert(err, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					})
				})
			}
		}
		authInfo.getAuthInfo().then((authCompanyInfo) => {
			that.comOptions = authCompanyInfo;
		});
		this.ordStatus = 4;//未确认
	},
	beforeMount () {
		this.gridOptions = {
			context: {
				componentParent: this
			},
			rowSelection: "multiple",
			enableColResize: true,
			localeText: localeText,
			//   getContextMenuItems: [],
			defaultColDef: {
				menuTabs: []
			},
			suppressRowClickSelection: true//单元格选中,false为选中整行
		};
		this.columnDefs = [
			{ headerName: "采购订单号", field: "purOrdNo", width: 160, checkboxSelection: true, cellRenderer: buttonLink },
			{
				headerName: "订单类型",
				field: "purOrdType",
				width: 120,
				cellRenderer: function (params) {
					for (var i in orderType) {
						if (orderType[i].value == params.value)
							return orderType[i].label;
					}
				}
			},
			{
				headerName: "订单日期",
				field: "purOrdDate",
				width: 120,
				cellRenderer: function (params) {
					if (params.value)
						return new Date(Date.parse(params.value.replace(/-/g, "/"))).Format("yyyy-MM-dd");
				}
			},
			{ headerName: "客户名称", width: 200, field: "companyName" },
			{ headerName: "采购组", width: 100, field: "purGroupName" },// purGroup
			{
				headerName: "发布日期",
				field: "releaseDate",
				width: 120,
				cellRenderer: function (params) {
					if (params.value)
						return new Date(Date.parse(params.value.replace(/-/g, "/"))).Format("yyyy-MM-dd");
				}
			},
			{
				headerName: "确认日期",
				field: "confirmDate",
				width: 120,
				cellRenderer: function (params) {
					if (params.value)
						return new Date(Date.parse(params.value.replace(/-/g, "/"))).Format("yyyy-MM-dd");
				}
			},
			{ headerName: "确认人", field: "confirmer", width: 120, tooltipField: 'confirmer' }
		]
	},
	components: {
		agGridComponents,
		purOrderDetailsComponents
	},
	watch: {
		selCompanyCode () {
			this.currentPage = 1;
			this.search();
		},
		purOrdNo (val) {
			if (val) {
				this.textChange = 1;
			} else {
				this.textChange = 0;
			}
		},
		beginDate () {
			this.current = 1;
		},
		endDate () {
			this.current = 1;
		}
	},
	methods: {
		//  限制开始日期选择范围
		disabledStartDate (time) {
			if (this.endDate)
				return time.getTime() > new Date(this.endDate).getTime();
		},
		//  限制结束日期选择范围
		disabledEndDate (time) {
			if (this.beginDate)
				return time.getTime() < (new Date(this.beginDate).getTime() - 3600 * 1000 * 24);
		},
		cancelComponents (msg) {
			if (msg) {
				var that = this;
				this.componentsShow = false;
				this.diaShow = true;
				this.$nextTick(_ => {
					//   that.$refs.agGridComponent.gridOptions.api.sizeColumnsToFit();
					that.$refs.agGridComponent.gridOptions.api.hideOverlay();
					that.search();
				});
			}
		},
		setClickValue (value) {
			if (value.data.delFlag == 0) {
				this.getClickValue = value.data;
				this.diaShow = false;
				this.componentsShow = true;
			} else if (value.data.delFlag == 1) {
				this.$alert("订单已退回或被强制收回,不能进行后续操作！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
			}
		},
		companyChange () {
			this.search();
		},
		handleSizeChange (params) {
			this.current = 1;
			this.currentPage = 1;
			this.pageSize = params;
			this.search();
		},
		handleCurrentChange (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.handleCurrentChange(this.currentPage);
		},
		handleNextClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.handleCurrentChange(this.currentPage);
		},
		search (flag) {
			var obj = {};
			var that = this;
			obj.eqParams = {};
			obj.geParams = {};
			obj.leParams = {};
			obj.likeParams = {};
			if (flag === 1)
				this.currentPage = 1;
			obj.current = this.currentPage;//this.currentPage;
			obj.size = this.pageSize;
			if (this.beginDate != "" && this.beginDate != null && this.endDate != "" && this.endDate != null) {
				if (this.beginDate > this.endDate) {
					this.$alert("提示：开始日期不能大于结束日期", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return;
				}
			}
			if (this.supplierInfo.length > 0) {
				obj.eqParams.supplierCode = this.supplierInfo[0].supplierCode;
			} else {
				this.$alert("供应商编码不能为空", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
				return
			}
			obj.eqParams.billState = 1;
			obj.eqParams.delFlag = this.stateCode
			if (this.selCompanyCode)
				obj.eqParams.companyCode = this.selCompanyCode.companyCode;
			if (this.purOrdNo)
				obj.likeParams.purOrdNo = this.purOrdNo;
			if (this.beginDate != "" && this.beginDate != null)
				obj.geParams.createDate = this.beginDate;
			else if ((this.beginDate == "" || this.beginDate == null) && this.endDate != "" && this.endDate != null)
				obj.geParams.createDate = this.endDate;

			if (this.endDate != "" && this.endDate != null)
				obj.leParams.createDate = this.endDate;
			else if ((this.endDate == "" || this.endDate == null) && this.beginDate != "" && this.beginDate != null)
				obj.leParams.createdate = this.beginDate;
			if (this.releaseDate.start) {
				obj.geParams.releaseDate = new Date(this.releaseDate.start).Format('yyyy-MM-dd 00:00:00')
			}
			if (this.releaseDate.end) {
				obj.leParams.releaseDate = new Date(this.releaseDate.end).Format('yyyy-MM-dd 23:59:59')
			}
			this.$refs.agGridComponent.loading = true;
			this.btnDis = true;
			_Api.getConfirmedPurOrder(obj).then(function (response) {
				that.btnDis = false;
				that.current = 0;
				that.textChange = 0;
				var data = response.data;
				if (data.code == "1") {
					if (data.total > 0) {
						that.rowData = data.rows.splice(0);
						that.$refs.agGridComponent.gridOptions.api.setRowData(that.rowData);
						that.total = data.total;
					} else {
						that.$refs.agGridComponent.gridOptions.api.setRowData([]);
						that.total = 0;
					}
					that.$refs.agGridComponent.loading = false;
				} else {
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
					that.$refs.agGridComponent.loading = false;
					that.$refs.agGridComponent.gridOptions.api.setRowData([]);
					that.total = 0;
				}
			}).catch((err) => {
				that.$alert(err.data.message, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
				that.$refs.agGridComponent.loading = false;
				that.$refs.agGridComponent.gridOptions.api.setRowData([]);
				that.total = 0;
				that.current = 0;
				that.textChange = 0;
			})
		},
		// 退回
		takeBack () {
			var rows = this.$refs.agGridComponent.gridOptions.api.getSelectedRows();
			var that = this;
			if (rows.length <= 0) {
				this.$alert("提示：请选择需要收回的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			var obj = {
				purOrdNo: rows[0].purOrdNo,
				id: rows[0].id
			}
			_Api.recyclePurOrder(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					that.$message({
						message: "操作成功",
						type: "success"
					});
					that.$refs.agGridComponent.gridOptions.api.updateRowData({
						remove: rows
					});
				} else {
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				}
			}).catch(() => {
				that.$alert("操作失败2！", {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
			})
		}
	}
}
</script>